<template>
	<view class="container">
		<view class="top-section">
			<view class="scan-section">
				<view class="input-box">
					<input 
						auto-focus="true" 
						ref="startLocationInput" 
						v-model="startLocationCode" 
						class="scan-input"
						placeholder="请输入或扫描起点库位"
					/>
					<image 
						v-if="startLocationCode" 
						class="input-icon clear-icon" 
						src="/static/images/clear.png" 
						@click="clearStartLocation"
					/>
					<image 
						class="input-icon scan-icon" 
						src="/static/images/scan.png"
						@click="handleScan('start')"
					/>
				</view>
				<view class="input-box">
					<input 
						ref="endLocationInput" 
						v-model="endLocationCode" 
						class="scan-input"
						placeholder="请输入或扫描终点库位"
					/>
					<image 
						v-if="endLocationCode" 
						class="input-icon clear-icon" 
						src="/static/images/clear.png" 
						@click="clearEndLocation"
					/>
					<image 
						class="input-icon scan-icon" 
						src="/static/images/scan.png"
						@click="handleScan('end')"
					/>
				</view>
			</view>
		</view>

		<view class="fixed-bottom">
			<button class="submit-btn" @tap="handleSubmit">调 库</button>
		</view>
	</view>
</template>

<script>
	import {
	  transferShelf
	} from "./api"
	
	export default {
		data() {
			return {
				startLocationCode: "", // 起点库位编码
				endLocationCode: "",   // 终点库位编码
				trayCode: "",          // 托盘编码
			};
		},
		methods: {
			handleScan(type) {
				// TODO: 实现扫码功能
				this.$modal.msg(`请扫描${type === 'start' ? '起点' : '终点'}库位`)
			},
			
			clearStartLocation() {
				this.startLocationCode = '';
			},
			
			clearEndLocation() {
				this.endLocationCode = '';
			},
			
			clearTrayCode() {
				this.trayCode = '';
			},
			
			resetForm() {
				this.startLocationCode = '';
				this.endLocationCode = '';
				this.trayCode = '';
			},
			
			validateForm() {
				if (!this.startLocationCode) {
					this.$modal.msg("请扫描起点库位");
					return false;
				}
				if (!this.endLocationCode) {
					this.$modal.msg("请扫描终点库位");
					return false;
				}
				return true;
			},
			
			handleSubmit() {
				if (!this.validateForm()) {
					return;
				}
				
				this.$modal.confirm('确认进行库位调整？').then(() => {
					this.$modal.closeLoading();
					
					transferShelf({
						startPosition: this.startLocationCode,
						endPosition: this.endLocationCode,
					}).then(res => {
						if (res.code === 200) {
							this.$modal.confirmClose(res.msg);
						} else {
							res.msg && this.$modal.confirmClose(res.msg);
						}
						this.resetForm();
					}).catch(err => {
						this.$modal.msg('操作失败，请重试');
					});
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #F0F2F5;
		position: relative;
		padding-bottom: 240rpx;

		.top-section {
			padding: 30rpx 20rpx;
			background: linear-gradient(180deg, #99cfd6 0%, #4599ad 100%);
		}

		.scan-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
			margin-bottom: 20rpx;

			.input-box {
				position: relative;
				margin-bottom: 20rpx;

				&:last-child {
					margin-bottom: 0;
				}
				
				.scan-input {
					background-color: #F8F9FA;
					height: 80rpx;
					width: 100%;
					padding: 0 120rpx 0 24rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
					border: 2rpx solid #E5E6EB;

					&::placeholder {
						color: #C9CDD4;
					}
				}

				.input-icon {
					position: absolute;
					width: 40rpx;
					height: 40rpx;
					top: 50%;
					transform: translateY(-50%);
					z-index: 1;
					
					&.clear-icon {
						right: 70rpx;
					}
					
					&.scan-icon {
						right: 20rpx;
					}
				}
			}
		}

		.fixed-bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: #FFFFFF;
			padding: 24rpx 20rpx;
			box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);

			.submit-btn {
				height: 88rpx;
				line-height: 88rpx;
				background: #4599ad;
				color: #FFFFFF;
				border-radius: 10rpx;
				font-size: 32rpx;
				text-align: center;
				font-weight: 500;
				box-shadow: 0 4rpx 12rpx rgba(46, 92, 254, 0.3);

				&:active {
					transform: scale(0.98);
				}
			}
		}
	}

	.nodata-label {
		text-align: center;
		margin-top: 40rpx;
		color: #999999;
	}
</style>